package com.m1905hd.user.service.impl

import com.m1905hd.common.util.JSONUtil
import com.m1905hd.user.entity.Resource
import com.m1905hd.user.model.BuiMenu
import com.m1905hd.user.service.ResourceService
import org.springframework.stereotype.Service
import org.springframework.util.StringUtils

/**
 *
 * @author weiwei.huang
 * @email ihuangweiwei@163.com
 * @date 15/3/31
 */
@Service
class ResourceServiceImpl extends BaseServiceImpl<Resource> implements ResourceService {

    static String NAMESPACE = (Resource.class.getName() + ".").intern();

    @Override
    Set<String> findPermissions(Set<Integer> ids) {
        Set<String> permissions = new HashSet<String>();
        ids.each { resourceId ->
            Resource resource = find(resourceId);
            if (resource != null && !StringUtils.isEmpty(resource.getPermission())) {
                permissions.add(resource.getPermission());
            }
        }
        return permissions;
    }

    @Override
    List<Resource> findMenus(Set<String> permissions) {
        return null
    }

    @Override
    BuiMenu getBuiMenuConfig(Set<String> permissions) {
        List<Resource> allResources = findAll();
        Map<Integer, Resource> resourceMap = new HashMap<>()
        allResources.each {
            resourceMap.put(it.id, it)
        }
        List<Resource> menus = new ArrayList<Resource>();
        for (Resource resource : allResources) {

            if (resource.getType() != Resource.ResourceType.menu) {
                continue;
            }
            if (!permissions.contains("*.*") && !permissions.contains(resource.getPermission())) {
                continue
            }
            menus.add(resource);
        }


        List<Resource> rootResource = new ArrayList<>()
        menus.each { resource ->
            String[] parentIds = resource.getParentIds().split(",")
            Resource r = resourceMap.get(parentIds[0] as int)
            if (parentIds.size() > 0 && !rootResource.contains(r)) {
                rootResource.add(r)
            }
            if (parentIds.size() == 2 && !r.getChildResource().contains(resourceMap.get(parentIds[1] as int))) {
                r.getChildResource().add(resourceMap.get(parentIds[1] as int))

            }
            r = resourceMap.get(parentIds[1] as int)
            r.getChildResource().add(resource)

        }

        def title = new ArrayList()
        def list = new ArrayList()
        rootResource.each {
            def menu = new ArrayList()
            def info = [id: it.id + "", menu: menu]
            title.add(it.name)
            List<Resource> childResource = it.getChildResource();
            if (childResource) {
                for (Resource r : childResource) {
                    def items = new ArrayList()
                    def m = [text: r.name, items: items]

                    List<Resource> childResourceList = r.getChildResource()
                    if (childResourceList) {

                        childResourceList.each { child ->
                            items.add([id: child.id + "", text: child.name, href: child.url])
                        }
                    }
                    menu.add(m)
                }
            }
            list.add(info)
        }


        BuiMenu buiMenu = new BuiMenu(title, JSONUtil.beanToJson(list))
        return buiMenu
    }

    @Override
    List<Resource> findAll() {
        return getSqlSession().selectList(NAMESPACE + "findAll")
    }

    @Override
    Resource findByParentId(int id) {
        return getSqlSession().selectOne(NAMESPACE + "findByParentId", id)
    }

    @Override
    String getNameSpace() {
        return NAMESPACE
    }
}
